package com.jdaw.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jdaw.reggie.common.R;
import com.jdaw.reggie.entity.Employee;
import com.jdaw.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    /*
    * 员工登录
    */
    @PostMapping("/login")//前端发送的是post请求
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //httpServletRequest因为需要将用户名存入session，可以随时获取用户
            String password= DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
            LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper();
            queryWrapper.eq(Employee::getUsername,employee.getUsername());
            Employee emp=employeeService.getOne(queryWrapper);
            if(emp==null){
                return R.error("登录失败");
            }

            if(!emp.getPassword().equals(password)){
                return R.error("登录失败");
            }
            //需要查看员工状态status为1可以登录
            if(emp.getStatus()==0){
                return R.error("账户已经禁用");
            }

            request.getSession().setAttribute("employee",emp.getId());
            return R.success(emp);
    }

    /*
    * 用户登出功能
    * */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //需要清理session里的用户id，然后跳转
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /*
    *添加员工
    */
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("员工信息：{}",employee.toString());
        String password=DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        //获取当前登录用户的id,设置创建员工的管理员id
//        Long empId=(Long)request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("新增员工成功");
    }

    /**
     * 分页
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //构造分页构造器
        Page page1=new Page(page,pageSize);
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.like(!StringUtils.isEmpty(name),Employee::getUsername, name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(page1,queryWrapper);
        return R.success(page1);
    }

    /**
     * 更新员工信息
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser((Long)request.getSession().getAttribute("employee"));
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    /**
     * 获取员工信息
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee=employeeService.getById(id);
        if(employee!=null){
            return R.success(employee);
        }
        return R.error("该用户无法找到");
    }
}
